/**
  ******************************************************************************
  * @file    APP_SPEED.c
  * @author  Feirui_Joe
  * @version V0.1
  * @date    07/26/2010
  * @brief   ACAD Handling
  ******************************************************************************
  * @copy
  *
  * <h2><center>&copy; COPYRIGHT 2010 Feirui</center></h2>
  */ 



/**
  * @brief  INCLUDE FILES.
  */
#ifndef	APP_SPEED_GLOBALS
#define	APP_SPEED_GLOBALS
#include <includes.h>






/* Initial Speed Loss Alarm */
void InitalSpeedInputsAlarmOperation(uint8_t InitOrd)
{
	FillShareBufferDataContent(App_Sem_INPUTCTRSTA ,CfgOperaType_OSSem,&ControlStatus_Speed,InitOrd,1);
	SetOSFlagsForNewInform(App_Flags_DSTIN,OSDSTINFlags_SpeedRequest);
}





static void InitiSpeedAlarmStatus(void);
static void InitiSpeedLowStatus(void);
static void InitiSpeedGenStatus(void);
/**
  * @brief  Status Reset for DSTIN
  * @param  none
  * @return none.
  * @Caller POR
  * @Note   none.
  */
void PORInputsStaReset_SPEED(void)
{
	InitalSpeedInputsAlarmOperation(CtrStaOrd_InitSpeedAlarmClr);
}


/**
  * @brief  Local RAM Reset for DSTIN
  * @param  none
  * @return none.
  * @Caller POR
  * @Note   none.
  */
void PORLocalRAMReset_SPEED(void)
{
  /* Initail IRQ RAM */
  PICKIN_PulseCnt=0;
  PICKIN_TimerCNT=0;
  PICKIN_OVTimCNT=0;
  
  SpeedDataReadyFlag=DEF_FALSE;
  SpeedRotationValue=0;

  InitiSpeedAlarmStatus();
  InitiSpeedGenStatus();
}


static void InitiSpeedAlarmStatus(void)
{
  SpeedAlarmSta_OverShut  =DEF_FALSE;
  SpeedAlarmSta_OverWarn  =DEF_FALSE;
  SpeedAlarmSta_HighShut  =DEF_FALSE;
  SpeedAlarmSta_HighWarn  =DEF_FALSE;
  SpeedAlarmSta_LowWarn   =DEF_FALSE;
  SpeedAlarmSta_LowShut   =DEF_FALSE;
  SpeedAlarmSta_LossSignal=DEF_FALSE;
  SpeedAlarmTiCnt_OverShut  =0;
  SpeedAlarmTiCnt_OverWarn  =0;
  SpeedAlarmTiCnt_HighShut  =0;
  SpeedAlarmTiCnt_HighWarn  =0;
  SpeedAlarmTiCnt_LowWarn   =0;
  SpeedAlarmTiCnt_LowShut   =0;
  SpeedAlarmTiCnt_LossSignal=0;
}
static void InitiSpeedLowStatus(void)
{
  SpeedAlarmSta_LowWarn   =DEF_FALSE;
  SpeedAlarmSta_LowShut   =DEF_FALSE;
  SpeedAlarmTiCnt_LowWarn   =0;
  SpeedAlarmTiCnt_LowShut   =0;
}
static void InitiSpeedGenStatus(void)
{
  SpeedAlarmSta_GenRunning=DEF_FALSE;
  SpeedAlarmSta_CrankOK   =DEF_FALSE;
  SpeedAlarmSta_FastLoad  =DEF_FALSE;
  SpeedAlarmTiCnt_GenRunning=0;
  SpeedAlarmTiCnt_CrankOK   =0;
  SpeedAlarmTiCnt_FastLoad  =0;
}


/**
  * @brief  Global RAM Reset for DSTIN
  * @param  none
  * @return none.
  * @Caller POR
  * @Note   none.
  */
void PORGlobalRAMReset_SPEED(void)
{
	
	
}



                                         /********************************************/
                                         /*********Speed Inputs Layer  *************/
                                         /********************************************/
static uint8_t SpeedInputsWorkLayerHandle(void);
static uint8_t SpeedInputsRangeHandle(void);
static uint8_t SpeedInputsActionHandle(void);
static uint8_t SpeedInputsInitLayerHandle(uint8_t InputCtrSta);

/* Speed Inputs Layer Handling */
uint8_t SpeedInputsLayerHandle(OS_FLAGS os_value)
{
	uint8_t  InputCtrSta;
	uint8_t  Status=DEF_FALSE;
	
	/************************************/
  /* Speed Workload--Speed Signal */
  if(os_value&OSDSTINFlags_SpeedSignal)
  	SpeedInputsWorkLayerHandle();
  
  
  /************************************/
  /* Speed Workload--Speed Request */
  if(os_value&OSDSTINFlags_SpeedRequest)
  {
  	/* Get Control Status */
	  ExchangeShareBufferDataContent(App_Sem_INPUTCTRSTA,CfgOperaType_OSSem,&InputCtrSta,&ControlStatus_Speed,1);
  	
  	if(SpeedInputsInitLayerHandle(InputCtrSta))
    	Status|=SpeedInputsActionHandle();
  	
  	/* Reset Control Status */
    FillShareBufferDataContent(App_Sem_INPUTCTRSTA,CfgOperaType_OSSem,&ControlStatus_Speed,CtrStaOrd_NormalWork,1);
  }
  
  
  /************************************/
  /* Speed Workload--Speed Timing */
  if(os_value&OSDSTINFlags_SpeedTiming)
  {
    if(SpeedInputsRangeHandle())
    	Status|=SpeedInputsActionHandle();
  }
  
  
	return(Status);
}


/* Speed Inputs Work Layer Handling */
static uint8_t SpeedInputsWorkLayerHandle(void)
{
	uint64_t CalTemp1;
	uint16_t TempwheelTeeth;
	uint16_t SpeedVal;
	
	/***********************/
	/* Get Data Value */
  if(SpeedDataReadyFlag==DEF_FALSE)
  	return(DEF_FALSE);
  SpeedTempReadyPulse=SpeedDataReadyPulse;
  SpeedTempReadyTicnt=SpeedDataReadyTicnt;
  SpeedDataReadyFlag=DEF_FALSE;
  
	
	/***************************/
	/* Calculate Speed Value */
	if(SpeedTempReadyPulse&SignalBufferLossSpeedBit)
	{
	  SpeedRotationValue=0;
	  SpeedVal=0;
	  CalTemp1=0;
	  WriteDataBaseDataContent(AdrCommSta_SpeedFreq,(uint16_t *)&CalTemp1,4);
	}
	else
	{
		/* Get Cfgs   */
  	ReadDataBaseDataContent(Adr_EngineFlywheelTeeth,&TempwheelTeeth,1);
		
		/* Cal Freq*600*Pulse /Ticnt / Wheel */
	  CalTemp1=((uint64_t)TIM3CountCLK_FREQ*600);
	  CalTemp1*=SpeedTempReadyPulse;
	  CalTemp1/=(SpeedTempReadyTicnt-SignalTimerCountOffset);
	  WriteDataBaseDataContent(AdrCommSta_SpeedFreq,(uint16_t *)&CalTemp1,4);
	  CalTemp1/=TempwheelTeeth;
	  
	  SpeedRotationValue=(uint32_t)CalTemp1;
	  
		if(CalTemp1>65535)
		  SpeedVal=65535;
		else
			SpeedVal=(uint16_t)CalTemp1;
		
		AdjustValueByCfgRangeLimit(&SpeedVal,AdrBasic_Enginespeed);
	}
	
	/* Update Speed Value */
	if(GetDataBaseDataContent(Adr_EngineMagneticPickupOpt))
	  FillDataBaseDataContent(AdrBasic_Enginespeed,SpeedVal,1);
	
	return(DEF_TRUE);
}




/* Speed Inputs Range */
static uint8_t SpeedInputsRangeHandle(void)
{
	uint16_t SpeedPcfgBufferAlarm[SpeedPcfgBFTOT_AlarmNum];
	uint16_t EnginePcfgBufferCrank[EnginePcfgBFTOT_CrankNum];
	uint8_t  Status=DEF_FALSE;
	uint32_t HighShutVal,HighWarnTrip,HighWarnReturn,LowWarnTrip,LowWarnReturn,LowShutVal,FastLoadTrip;
	
	/********************/
	/* Get Speed Pcfg */
	ReadDataBaseDataContent(Adr_EngineSpeedOnLoad,&SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_SpeedOnLoad],SpeedPcfgBFTOT_AlarmNum-1);
	GetAuxiConfigGenParams(                       &SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_RatedSpeed ],AuxCfgGenParamOrd_RatedSpeed);
	ReadDataBaseDataContent(Adr_CrankDisFreqOpt,EnginePcfgBufferCrank,EnginePcfgBFTOT_CrankNum);
	uint16_t TempMagneticPickupOpt=GetDataBaseDataContent(Adr_EngineMagneticPickupOpt);
	
  HighShutVal   =(uint32_t)SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_RatedSpeed]*(uint32_t)SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_OverSpeedShutdownTrip   ]/100;
  HighWarnTrip  =(uint32_t)SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_RatedSpeed]*(uint32_t)SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_OverSpeedPreAlarmTrip   ]/100;
  HighWarnReturn=(uint32_t)SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_RatedSpeed]*(uint32_t)SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_OverSpeedPreAlarmReturn ]/100;
  LowWarnTrip   =(uint32_t)SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_RatedSpeed]*(uint32_t)SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_UnderSpeedPreAlarmTrip  ]/100;
  LowWarnReturn =(uint32_t)SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_RatedSpeed]*(uint32_t)SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_UnderSpeedPreAlarmReturn]/100;
  LowShutVal    =(uint32_t)SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_RatedSpeed]*(uint32_t)SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_UnderSpeedShutdownTrip  ]/100;
  FastLoadTrip  =LowShutVal;
  
	/********************/
	/* Speed High Shut */
	AlarmSmartParam.Option   =SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_OverSpeedShutdownOpt]&&TempMagneticPickupOpt;
	AlarmSmartParam.OperaType=AlarmOperaType_HighShut;
	AlarmSmartParam.OffType  =AlarmOperaType_DisableInActive;
	AlarmSmartParam.Trip     =HighShutVal;
	AlarmSmartParam.Return   =0;
	AlarmSmartParam.Value    =SpeedRotationValue;
	AlarmSmartParam.Status   =&SpeedAlarmSta_HighShut;
	AlarmSmartParam.Ticnt    =&SpeedAlarmTiCnt_HighShut;
	AlarmSmartParam.TiLimit  =SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_OverSpeedShutdownDelay]*SpeedInputTimingFreq;
	AlarmSmartParam.Actcnt   =NULL;
	Status|=InputsAlarmSmartHandling(&AlarmSmartParam);
  
	/********************/
	/* Speed Over Shut */
//AlarmSmartParam.Option   =SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_OverSpeedShutdownOpt]&&TempMagneticPickupOpt;
//AlarmSmartParam.OperaType=AlarmOperaType_HighShut;
//AlarmSmartParam.OffType  =AlarmOperaType_DisableInActive;
	AlarmSmartParam.Trip     =(uint32_t)HighShutVal*(uint32_t)(SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_OverSpeedOvershoot]+100)/100;
//AlarmSmartParam.Return   =0;
//AlarmSmartParam.Value    =SpeedRotationValue;
	AlarmSmartParam.Status   =&SpeedAlarmSta_OverShut;
	AlarmSmartParam.Ticnt    =&SpeedAlarmTiCnt_OverShut;
//AlarmSmartParam.TiLimit  =SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_OverSpeedShutdownDelay]*SpeedInputTimingFreq;
//AlarmSmartParam.Actcnt   =NULL;
	Status|=InputsAlarmSmartHandling(&AlarmSmartParam);
  
	/********************/
	/* Speed High Warn */
	AlarmSmartParam.Option   =SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_OverSpeedPreAlarmOpt]&&TempMagneticPickupOpt;
	AlarmSmartParam.OperaType=AlarmOperaType_HighWarn;
	AlarmSmartParam.OffType  =AlarmOperaType_DisableInActive;
	AlarmSmartParam.Trip     =HighWarnTrip  ;
	AlarmSmartParam.Return   =HighWarnReturn;
//AlarmSmartParam.Value    =SpeedRotationValue;
	AlarmSmartParam.Status   =&SpeedAlarmSta_HighWarn;
	AlarmSmartParam.Ticnt    =&SpeedAlarmTiCnt_HighWarn;
	AlarmSmartParam.TiLimit  =SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_OverSpeedPreAlarmDelay]*SpeedInputTimingFreq;
//AlarmSmartParam.Actcnt   =NULL;
	Status|=InputsAlarmSmartHandling(&AlarmSmartParam);
	
	/********************/
	/* Speed Over Warn */
//AlarmSmartParam.Option   =SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_OverSpeedPreAlarmOpt]&&TempMagneticPickupOpt;
//AlarmSmartParam.OperaType=AlarmOperaType_HighWarn;
//AlarmSmartParam.OffType  =AlarmOperaType_DisableInActive;
	AlarmSmartParam.Trip     =(uint32_t)HighWarnTrip  *(uint32_t)(SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_OverSpeedOvershoot]+100)/100;
	AlarmSmartParam.Return   =(uint32_t)HighWarnReturn*(uint32_t)(SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_OverSpeedOvershoot]+100)/100;
//AlarmSmartParam.Value    =SpeedRotationValue;
	AlarmSmartParam.Status   =&SpeedAlarmSta_OverWarn;
	AlarmSmartParam.Ticnt    =&SpeedAlarmTiCnt_OverWarn;
//AlarmSmartParam.TiLimit  =SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_OverSpeedPreAlarmDelay]*SpeedInputTimingFreq;
//AlarmSmartParam.Actcnt   =NULL;
	Status|=InputsAlarmSmartHandling(&AlarmSmartParam);
	
	/********************/
	/* Speed Low Warn */
	AlarmSmartParam.Option   =SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_UnderSpeedPreAlarmOpt]&&TempMagneticPickupOpt;
	AlarmSmartParam.OperaType=AlarmOperaType_LowWarn;
	AlarmSmartParam.OffType  =AlarmOperaType_DisableInActive;
	AlarmSmartParam.Trip     =LowWarnTrip  ;
	AlarmSmartParam.Return   =LowWarnReturn;
//AlarmSmartParam.Value    =SpeedRotationValue;
	AlarmSmartParam.Status   =&SpeedAlarmSta_LowWarn;
	AlarmSmartParam.Ticnt    =&SpeedAlarmTiCnt_LowWarn;
	AlarmSmartParam.TiLimit  =SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_UnderSpeedPreAlarmDelay]*SpeedInputTimingFreq;
//AlarmSmartParam.Actcnt   =NULL;
	Status|=InputsAlarmSmartHandling(&AlarmSmartParam);
  
	/********************/
	/* Speed Low Shut */
	AlarmSmartParam.Option   =SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_UnderSpeedShutdownOpt]&&TempMagneticPickupOpt;
	AlarmSmartParam.OperaType=AlarmOperaType_LowShut;
	AlarmSmartParam.OffType  =AlarmOperaType_DisableInActive;
	AlarmSmartParam.Trip     =LowShutVal;
	AlarmSmartParam.Return   =0;
//AlarmSmartParam.Value    =SpeedRotationValue;
	AlarmSmartParam.Status   =&SpeedAlarmSta_LowShut;
	AlarmSmartParam.Ticnt    =&SpeedAlarmTiCnt_LowShut;
	AlarmSmartParam.TiLimit  =SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_UnderSpeedShutdownDelay]*SpeedInputTimingFreq;
//AlarmSmartParam.Actcnt   =NULL;
	Status|=InputsAlarmSmartHandling(&AlarmSmartParam);
	
	/********************/
	/* Speed Loss Signal */
	AlarmSmartParam.Option   =TempMagneticPickupOpt;
	AlarmSmartParam.OperaType=AlarmOperaType_LowShut;
	AlarmSmartParam.OffType  =AlarmOperaType_DisableInActive;
	AlarmSmartParam.Trip     =SpeedLossSignalRPMLimit;
	AlarmSmartParam.Return   =0;
//AlarmSmartParam.Value    =SpeedRotationValue;
	AlarmSmartParam.Status   =&SpeedAlarmSta_LossSignal;
	AlarmSmartParam.Ticnt    =&SpeedAlarmTiCnt_LossSignal;
	AlarmSmartParam.TiLimit  =SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_LossOfSpeedDelay]*SpeedInputTimingFreq/10;
//AlarmSmartParam.Actcnt   =NULL;
	Status|=InputsAlarmSmartHandling(&AlarmSmartParam);
	
	/********************/
	/* Speed Running */
	AlarmSmartParam.Option   =TempMagneticPickupOpt;
	AlarmSmartParam.OperaType=AlarmOperaType_HighShut;
	AlarmSmartParam.OffType  =AlarmOperaType_DisableInActive;
	AlarmSmartParam.Trip     =SpeedGenRunningRPMLimit;
	AlarmSmartParam.Return   =0;
//AlarmSmartParam.Value    =SpeedRotationValue;
	AlarmSmartParam.Status   =&SpeedAlarmSta_GenRunning;
	AlarmSmartParam.Ticnt    =&SpeedAlarmTiCnt_GenRunning;
	AlarmSmartParam.TiLimit  =SpeedGenRunningTimLimit*SpeedInputTimingFreq/10;
//AlarmSmartParam.Actcnt   =NULL;
	Status|=InputsAlarmSmartHandling(&AlarmSmartParam);	
  
	/********************/
	/* Speed Crank */
	AlarmSmartParam.Option   =EnginePcfgBufferCrank[EnginePcfgBFOrd_CrankDisSpeedOpt]&&TempMagneticPickupOpt;
	AlarmSmartParam.OperaType=AlarmOperaType_HighShut;
	AlarmSmartParam.OffType  =AlarmOperaType_DisableInActive;
	AlarmSmartParam.Trip     =(uint32_t)SpeedPcfgBufferAlarm[SpeedPcfgBFOrd_RatedSpeed]*(uint32_t)EnginePcfgBufferCrank[EnginePcfgBFOrd_CrankDisSpeedVal]/100;
	AlarmSmartParam.Return   =0;
//AlarmSmartParam.Value    =SpeedRotationValue;
	AlarmSmartParam.Status   =&SpeedAlarmSta_CrankOK;
	AlarmSmartParam.Ticnt    =&SpeedAlarmTiCnt_CrankOK;
	AlarmSmartParam.TiLimit  =SpeedCrankTimLimit*SpeedInputTimingFreq/10;
//AlarmSmartParam.Actcnt   =NULL;
	Status|=InputsAlarmSmartHandling(&AlarmSmartParam);	

	/********************/
	/*  FastLoad */
	AlarmSmartParam.Option   =TempMagneticPickupOpt;
	AlarmSmartParam.OperaType=AlarmOperaType_HighShut;
	AlarmSmartParam.OffType  =AlarmOperaType_DisableActive;
	AlarmSmartParam.Trip     =FastLoadTrip;
	AlarmSmartParam.Return   =0;
//AlarmSmartParam.Value    =SpeedRotationValue;
	AlarmSmartParam.Status   =&SpeedAlarmSta_FastLoad;
	AlarmSmartParam.Ticnt    =&SpeedAlarmTiCnt_FastLoad;
	AlarmSmartParam.TiLimit  =SpeedFastLoadTimLimit*SpeedInputTimingFreq/10;
//AlarmSmartParam.Actcnt   =NULL;
	Status|=InputsAlarmSmartHandling(&AlarmSmartParam);	
	
	return(Status);
}



/* Speed Inputs Action*/
static uint8_t SpeedInputsActionHandle(void)
{
  uint16_t TempBuffer[SpeedRawAlarmTOT_Num][2],i,j;
  
  /***************************************/
  /* Update Raw Alarm Buffer */
  /***************************************/
  
  /* Initial Workload Buffer */
  for(j=RawAlarm_SpeedLoss,i=0;i<SpeedRawAlarmTOT_Num;i++)
  {
    TempBuffer[i][CMDBFOrd_Sequence]=j++;
    TempBuffer[i][CMDBFOrd_OperaOpt]=BitOperaOpt_Clr;
  }
  
  /* Speed Raw Alarm */
  if(SpeedAlarmSta_LossSignal)
  {
  	/* Speed Loss Signal */
  	TempBuffer[SpeedRawAlarmOrd_SpeedLoss][CMDBFOrd_OperaOpt]=BitOperaOpt_Set;
  }
  else
  {
  	/* Speed Over Shoot */
  	if(SpeedAlarmSta_OverShut)
  	  TempBuffer[SpeedRawAlarmOrd_SpeedOverShut][CMDBFOrd_OperaOpt]=BitOperaOpt_Set;
  	else if(SpeedAlarmSta_OverWarn)
  		TempBuffer[SpeedRawAlarmOrd_SpeedOverWarn][CMDBFOrd_OperaOpt]=BitOperaOpt_Set;
    
    /* Speed Normal */
    if(SpeedAlarmSta_HighShut)
  	  TempBuffer[SpeedRawAlarmOrd_SpeedHighShut][CMDBFOrd_OperaOpt]=BitOperaOpt_Set;
  	else if(SpeedAlarmSta_HighWarn)
  		TempBuffer[SpeedRawAlarmOrd_SpeedHighWarn][CMDBFOrd_OperaOpt]=BitOperaOpt_Set;
    else if(SpeedAlarmSta_LowShut)
  		TempBuffer[SpeedRawAlarmOrd_SpeedLowShut ][CMDBFOrd_OperaOpt]=BitOperaOpt_Set;
    else if(SpeedAlarmSta_LowWarn)
  		TempBuffer[SpeedRawAlarmOrd_SpeedLowWarn ][CMDBFOrd_OperaOpt]=BitOperaOpt_Set;
  }
  
  BatchFlagsShareBufferDataContent(App_Sem_RAWALARM,CfgOperaType_OSSem,ShareBuffer_RawAlarm,(uint16_t *)TempBuffer,SpeedRawAlarmTOT_Num);
  
  
  /***************************************/
  /* Update Gen Running Sta */
  /***************************************/  
  
  if(SpeedAlarmSta_GenRunning)
    ModifyFlagShareBufferDataContent(App_Sem_GENSTATUS,CfgOperaType_OSSem,&ShareBuffer_GenRunSta,GenRunningStaOrd_Speed,BitOperaOpt_Set);
  else
  	ModifyFlagShareBufferDataContent(App_Sem_GENSTATUS,CfgOperaType_OSSem,&ShareBuffer_GenRunSta,GenRunningStaOrd_Speed,BitOperaOpt_Clr);


  /***************************************/
  /* Update Gen Crank Sta */
  /***************************************/  
  
  if(SpeedAlarmSta_CrankOK)
    ModifyFlagShareBufferDataContent(App_Sem_GENSTATUS,CfgOperaType_OSSem,&ShareBuffer_GenCrankSta,GenCrankStaOrd_Speed,BitOperaOpt_Set);
  else
  	ModifyFlagShareBufferDataContent(App_Sem_GENSTATUS,CfgOperaType_OSSem,&ShareBuffer_GenCrankSta,GenCrankStaOrd_Speed,BitOperaOpt_Clr);

  /***************************************/
  /* Update Gen Fast Load */
  /***************************************/  
  if(SpeedAlarmSta_FastLoad)
    ModifyFlagShareBufferDataContent(App_Sem_GENSTATUS,CfgOperaType_OSSem,&ShareBuffer_GenFastLoadSta,GenFastLoadStaOrd_Speed,BitOperaOpt_Set);
  else
  	ModifyFlagShareBufferDataContent(App_Sem_GENSTATUS,CfgOperaType_OSSem,&ShareBuffer_GenFastLoadSta,GenFastLoadStaOrd_Speed,BitOperaOpt_Clr);
  
  
  return(DEF_TRUE);
}



/* Speed Inputs Initial */
static uint8_t SpeedInputsInitLayerHandle(uint8_t InputCtrSta)
{
	/* Invail Control Sta */
	if((InputCtrSta<CtrStaOrd_InitSpeedAlarmClr)||(InputCtrSta>CtrStaOrd_InitSpeedCrankOK))
	  return(DEF_FALSE);
  
	if(InputCtrSta==CtrStaOrd_InitSpeedAlarmClr)
    InitiSpeedAlarmStatus();
	else if(InputCtrSta==CtrStaOrd_InitSpeedLowClr)
    InitiSpeedLowStatus();
	else if(InputCtrSta==CtrStaOrd_InitSpeedGenClr)
		InitiSpeedGenStatus();
	else if(InputCtrSta==CtrStaOrd_InitSpeedLossClr)
	{
		SpeedAlarmSta_LossSignal=DEF_FALSE;
		SpeedAlarmTiCnt_LossSignal=0;
	}
	
	return(DEF_TRUE);
}





                                      /***************************************/
                                      /*       Frequency handling            */
                                      /***************************************/

static uint32_t PICKIN_XcoordinateStart;
static uint32_t PICKIN_XcoordinateLast;

/**
  * @brief  This function handles TIM3 global interrupt request.
  * @param  None
  * @retval None
  */
void TIM3_IRQHandler(void)
{
	register uint8_t   TIMcntTempH;
	register uint16_t  TIMcntTempL,TIMCapturnCnt;
	register uint32_t  TIMcntCal1,TIMcntCal2;
  OS_CPU_SR  cpu_sr;
  
  OS_ENTER_CRITICAL();
  OSIntEnter();
  OS_EXIT_CRITICAL();
  
  /*******************************************/
  /* Timer Overflow */
  if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
    PICKIN_TimerCNT++;
  }
  
  /*******************************************/
  /* PICK_IN Input capture */
  if(TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
  	
  	/* Get Capture Input CNT */
  	TIMcntTempH=PICKIN_TimerCNT;
  	TIMCapturnCnt=TIM_GetCapture1(TIM3);
  	TIMcntCal1=TIMcntTempH;
  	TIMcntCal1<<=16;
  	TIMcntCal1+=TIMCapturnCnt;
  	
  	/* Get Current TIM CNT  */
  	if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
    {
      TIMcntTempH++;
      TIMcntTempL = TIM_GetCounter(TIM3);
  	}
  	else
  	{
  		TIMcntTempL = TIM_GetCounter(TIM3);
  	  if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
      {
        TIMcntTempH++;
        TIMcntTempL = TIM_GetCounter(TIM3);
  	  }
  	}
  	TIMcntCal2=TIMcntTempH;
  	TIMcntCal2<<=16;
  	TIMcntCal2+=TIMcntTempL;
  	
  	TIMcntCal2-=TIMcntCal1;
  	TIMcntCal2&=0x00ffffff;
  	if(TIMcntCal2>0x8fffff)
  	{
  		TIMcntCal1-=0x00010000;
  		TIMcntCal1&=0x00ffffff;
  	}
  	
  	/********************************/
  	/* One Cycle at less two Point */
  	if(PICKIN_PulseCnt==0)
  	{
  		/* First Signal */
  		PICKIN_XcoordinateStart=TIMcntCal1;
  		PICKIN_XcoordinateLast =TIMcntCal1;
  		PICKIN_PulseCnt=1;
  	}
  	else
  	{
  		/* Cal Signal Cycle */
  	  TIMcntCal2=TIMcntCal1-PICKIN_XcoordinateLast;
  	  TIMcntCal2&=0x00ffffff;
  	  if(TIMcntCal2<SignalCycleFilterLimit)
  	  	goto EndAdr_TIM3IRQHandler;
  		PICKIN_XcoordinateLast=TIMcntCal1;
  		
  	  /* Update Pulse number */
  	  PICKIN_PulseCnt++;
  	  if(PICKIN_PulseCnt>=SignalBufferReadyCntLimit)
  		  PICKIN_PulseCnt=SignalBufferReadyCntLimit;
  	  
  	  /* Data Ready condition */
  	  TIMcntCal2=TIMcntCal1-PICKIN_XcoordinateStart;
  	  TIMcntCal2&=0x00ffffff;
  	  if(TIMcntCal2>=SignalBufferReadyTimLimit)
  	  {
  	  	/* Inform New Speed Signal */
  	  	if(SpeedDataReadyFlag==DEF_FALSE)
  	  	{
  	  		SpeedDataReadyPulse=PICKIN_PulseCnt-1;
  	  		SpeedDataReadyTicnt=TIMcntCal2;
  	  		SpeedDataReadyFlag=DEF_TRUE;
  	  		
  	  		SetOSFlagsForNewInform(App_Flags_DSTIN,OSDSTINFlags_SpeedSignal);
  	  	}
  	  	
  	  	/* First Signal */
  		  PICKIN_XcoordinateStart=TIMcntCal1;
  		  PICKIN_XcoordinateLast =TIMcntCal1;
  		  PICKIN_PulseCnt=1;
  	  }
  	}
    
    /* Initial Freq Timer Overflow */
    TIM_SetCompare2(TIM3, TIMCapturnCnt + ENGSPEED_TIMOCPulse);
    PICKIN_OVTimCNT=0;
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
    goto EndAdr_TIM3IRQHandler;
  }
  
  /********************************************************/
  /* PICK_IN Input capture */
  if(TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
    
    TIMcntTempL = TIM_GetCounter(TIM3);
    TIM_SetCompare2(TIM3, TIMcntTempL + ENGSPEED_TIMOCPulse);
    
    /* Signal Timer Over */
  	PICKIN_OVTimCNT++;
  	if(PICKIN_OVTimCNT>=VALMSOverTime_PICK_IN)
  	{
  		/* Initial TimerOver counter */
  		PICKIN_OVTimCNT=0;
      PICKIN_PulseCnt=0;
      
      /* Inform loss Speed Signal */
      if(SpeedDataReadyFlag==DEF_FALSE)
  	  {
  	  	SpeedDataReadyPulse=SignalBufferLossSpeedBit;
  	  	SpeedDataReadyTicnt=SignalBufferReadyTimLimit;
  	  	SpeedDataReadyFlag=DEF_TRUE;
  	  	
  	  	SetOSFlagsForNewInform(App_Flags_DSTIN,OSDSTINFlags_SpeedSignal);
  	  }
  	}
  }
  
EndAdr_TIM3IRQHandler:
  
  OSIntExit();
}










/*$PAGE*/#endif	/*	APP_SPEED_GLOBALS	*/
